package com.simplify.ink.sample;

import com.simplify.ink.InkView;
import com.simplify.ink.sample.utils.PixelUtil;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.element.PixelMapElement;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.global.resource.NotExistException;
import ohos.multimodalinput.event.TouchEvent;
import java.io.IOException;

/**
 * description : MainAbility
 *
 * @since 2021-05-29
 */
public class MainAbility extends Ability implements Component.ClickedListener {
    private static final int ANIMATOR_DURATION = 200;

    private static final int SHOW_MENU = 100;

    private static final int HIDE_MENU = 200;

    private InkView mInkView;
    private AnimatorProperty menuLayoutHideAnimator;
    private AnimatorProperty menuLayoutShowAnimator;
    private DirectionalLayout menuLayout;
    private Image interpolationImage;
    private Image weightImage;
    private Component clickToCloseMenuLayout;
    private final MyEventHandler handler = new MyEventHandler(EventRunner.getMainEventRunner());

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
        initAnimator();
    }

    private void initView() {
        if (findComponentById(ResourceTable.Id_ink_view) instanceof InkView) {
            mInkView = (InkView) findComponentById(ResourceTable.Id_ink_view);
        }
        if (findComponentById(ResourceTable.Id_menu_layout) instanceof DirectionalLayout) {
            menuLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_menu_layout);
        }
        if (findComponentById(ResourceTable.Id_ig_interpolation) instanceof Image) {
            interpolationImage = (Image) findComponentById(ResourceTable.Id_ig_interpolation);
        }
        if (findComponentById(ResourceTable.Id_ig_weight) instanceof Image) {
            weightImage = (Image) findComponentById(ResourceTable.Id_ig_weight);
        }
        clickToCloseMenuLayout = findComponentById(ResourceTable.Id_bg_component);
        clickToCloseMenuLayout.setTouchEventListener((component, touchEvent) -> {
            if (menuLayout.getVisibility() != Component.HIDE) {
                if (touchEvent.getAction() == TouchEvent.PRIMARY_POINT_UP) {
                    setMenuLayoutVisible(false);
                }
                return true;
            }
            return false;
        });
        findComponentById(ResourceTable.Id_clear_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_menu_button).setClickedListener(this);
        findComponentById(ResourceTable.Id_interpolation_button).setClickedListener(this);
        findComponentById(ResourceTable.Id_weight_button).setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        int id = component.getId();
        if (id == ResourceTable.Id_clear_btn) {
            mInkView.clear();
        } else if (id == ResourceTable.Id_interpolation_button) {
            if (mInkView.hasFlag(InkView.FLAG_INTERPOLATION)) {
                mInkView.removeFlag(InkView.FLAG_INTERPOLATION);
                setBackground(interpolationImage, ResourceTable.Media_uncheck);
            } else {
                mInkView.addFlag(InkView.FLAG_INTERPOLATION);
                setBackground(interpolationImage, ResourceTable.Media_check);
            }
            setMenuLayoutVisible(false);
        } else if (id == ResourceTable.Id_weight_button) {
            if (mInkView.hasFlag(InkView.FLAG_RESPONSIVE_WIDTH)) {
                mInkView.removeFlag(InkView.FLAG_RESPONSIVE_WIDTH);
                setBackground(weightImage, ResourceTable.Media_uncheck);
            } else {
                mInkView.addFlag(InkView.FLAG_RESPONSIVE_WIDTH);
                setBackground(weightImage, ResourceTable.Media_check);
            }
            setMenuLayoutVisible(false);
        } else {
            if (id == ResourceTable.Id_menu_button) {
                setMenuLayoutVisible(true);
            }
        }
    }

    private void setBackground(Image image, int resource) {
        try {
            PixelMapElement pixelMapElement =
                    new PixelMapElement(getResourceManager().getResource(resource));
            image.setBackground(pixelMapElement);
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        }
    }

    private void initAnimator() {
        // 设置menu消失的动画
        menuLayoutHideAnimator = menuLayout.createAnimatorProperty();
        menuLayoutHideAnimator.alpha(0).setDuration(ANIMATOR_DURATION);
        // 设置menu显示的动画
        int offsetY = 0;
        menuLayoutShowAnimator = menuLayout.createAnimatorProperty();
        menuLayoutShowAnimator
                .moveFromX(PixelUtil.screenWidth()).moveToX((PixelUtil.screenWidth()) - (menuLayout.getWidth()))
                .moveFromY(-(menuLayout.getHeight() + offsetY)).moveToY(offsetY)
                .scaleXFrom(0).scaleX(1)
                .scaleYFrom(0).scaleY(1)
                .alphaFrom(0).alpha(1)
                .setDuration(ANIMATOR_DURATION);
    }

    /**
     * 设置菜单栏显示/隐藏
     *
     * @param visible 菜单栏是否可见
     */
    private void setMenuLayoutVisible(boolean visible) {
        if (visible) {
            handler.sendEvent(SHOW_MENU);
            menuLayout.invalidate();
            menuLayoutShowAnimator.start();
        } else {
            menuLayoutHideAnimator.start();
            handler.sendEvent(HIDE_MENU, ANIMATOR_DURATION);
        }
    }

    private class MyEventHandler extends EventHandler {
        private MyEventHandler(EventRunner runner) {
            super(runner);
        }

        // 重写实现processEvent方法
        @Override
        public void processEvent(InnerEvent event) {
            super.processEvent(event);
            switch (event.eventId) {
                case SHOW_MENU:
                    // 显示菜单栏
                    if (menuLayout != null) {
                        menuLayout.setVisibility(Component.VISIBLE);
                        clickToCloseMenuLayout.setVisibility(Component.VISIBLE);
                        menuLayout.scrollTo(100, 100);
                    }
                    break;
                case HIDE_MENU:
                    // 隐藏菜单栏
                    if (menuLayout != null) {
                        menuLayout.setVisibility(Component.HIDE);
                        clickToCloseMenuLayout.setVisibility(Component.HIDE);
                    }
                    break;
            }
        }
    }
}
